from nlp_tools.utils.model import convert_to_saved_model
from nlp_tools.tasks.classification.abc_model import ABCTaskModel
import os
import requests
import numpy as np
from nlp_tools.processors.tools import load_processors_from_model

model_save_path = '/home/qiufengfeng/nlp/train_models/classification/'
loaded_model = ABCTaskModel.load_model(model_save_path,no_models=True)

text_processor, label_processor = load_processors_from_model(model_save_path)

text = "这是一个测试"
tensor = text_processor.transform([text])

tensor = [{
    "Input-Token": i.tolist(),
    "Input-Segment": np.zeros(i.shape).tolist()
    } for i in tensor]




# predict
r = requests.post("http://localhost:8501/v1/models/classification:predict", json={"instances": tensor})
preds = r.json()['predictions']
print(preds)
predict = np.array(preds).argmax(-1)
print(label_processor.inverse_transform(predict))


'''
curl -d '{"Input-Segment": [1.0, 2.0, 5.0]}' \
   -X POST http://localhost:8501/v1/models/classification:predict'''
